Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make labels.NewRequirement returns aggregated field.ErrorList #97538

Merged

Conversation

lingsamuel
Copy link
Contributor

@lingsamuel lingsamuel commented Dec 28, 2020

Make labels.NewRequirement returns aggregated field.ErrorList, make nodeaffinity parsing functions use it

Signed-off-by: Ling Samuel lingsamuelgrace@gmail.com

What type of PR is this?

/kind feature

What this PR does / why we need it:

See #96167

Which issue(s) this PR fixes:

Fixes #96167

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 28, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @lingsamuel. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. and removed release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 28, 2020
@lingsamuel lingsamuel force-pushed the requirement-return-field-error branch 5 times, most recently from 9de087d to 2ee3655 Compare December 28, 2020 05:03
@lingsamuel
Copy link
Contributor Author

/cc @alculquicondor

@yuxiaobo96
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 28, 2020
@lingsamuel lingsamuel force-pushed the requirement-return-field-error branch 2 times, most recently from a98b154 to 0a3c5ca Compare December 28, 2020 07:20
@k8s-ci-robot k8s-ci-robot added the sig/apps Categorizes an issue or PR as relevant to SIG Apps. label Jan 7, 2021
@lingsamuel lingsamuel force-pushed the requirement-return-field-error branch from 7095b21 to 9438c59 Compare January 7, 2021 13:45
Copy link
Member

@alculquicondor alculquicondor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
for scheduling

Please squash

…odeaffinity parsing function use it

Signed-off-by: Ling Samuel <lingsamuelgrace@gmail.com>
@lingsamuel lingsamuel force-pushed the requirement-return-field-error branch from 96ed831 to a1f8dc4 Compare January 11, 2021 08:24
@lingsamuel
Copy link
Contributor Author

Commits rebased.

/cc @lavalamp

@lingsamuel
Copy link
Contributor Author

/retest

Field: "[2].matchExpressions[0]",
Detail: `invalid label key "invalid key": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
Field: "[2].matchExpressions[0].key",
Detail: `name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"name part" what other part of the key is there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "name part" generated by IsQualifiedName, here is the whole key (.key indicates it).

if !qualifiedNameRegexp.MatchString(name) {
errs = append(errs, "name part "+RegexError(qualifiedNameErrMsg, qualifiedNameFmt, "MyName", "my.name", "123-abc"))
}

func validateLabelKey(k string, path *field.Path) *field.Error {
if errs := validation.IsQualifiedName(k); len(errs) != 0 {
return field.Invalid(path, k, strings.Join(errs, "; "))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will a user understand this from the message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just remove "name part"?

@@ -218,8 +218,8 @@ func TestPreferredSchedulingTermsScore(t *testing.T) {
},
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "[2].matchExpressions[0]",
Detail: `invalid label key "invalid key": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
Field: "[2].matchExpressions[0].key",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple keys (multiple expressions), how does the user know which one this message is about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output contains field. It would like [[2].matchExpressions[0].key: Invalid value: REASON, [2].matchExpressions[1].key: Invalid value: REASON]

func (v *Error) Error() string {
return fmt.Sprintf("%s: %s", v.Field, v.ErrorBody())
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry to be so dense, but I've found that's often much more accurate to look at the actually rendered strings than mentally follow the string rendering code. Can you make a test that prints out the rendered error messages from the invalid cases and run it with and without your change here, so we can compare the difference? Or just manually put some invalid selectors through the API.

In a change like this I really want to be sure that the error messages are helping users more and not just making the code more regular.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would like:
image

test yaml:

apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchFields:
          - key: "metadata.name"
            operator: In
            values:
            - a
            - b
        - matchExpressions:
          - key: "invalid key"
            operator: In
            values:
            - b
            - c
  containers:
  - name: with-node-affinity
    image: k8s.gcr.io/pause:3.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kube-scheduler has a similar output, just not pretty print like kubectl, because kubectl formats a http response, kube-scheduler simply call .Error().
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR doesn't introduce this error format, it just makes labels.NewRequirement return all parsing errors.

Example (without/with this patch):
image

apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: KubeSchedulerConfiguration
profiles:
  - schedulerName: default-scheduler
  - schedulerName: test-err-scheduler
    pluginConfig:
    - name: NodeAffinity
      args:
        addedAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: "invalid key"
                operator: Gt
                values:
                - b
                - c
clientConnection:
  kubeconfig: kubeconfig

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for humoring me :)

...I wouldn't say this is amazingly clear, but it's maybe slightly better than it was, and I think people will be able to figure it out.

@lavalamp
Copy link
Member

I'd still like to see complete error messages as rendered for a human before signing off on this.

@lavalamp
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 13, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alculquicondor, lavalamp, lingsamuel

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 13, 2021
@k8s-ci-robot k8s-ci-robot merged commit a28c802 into kubernetes:master Jan 13, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Jan 13, 2021
@lingsamuel lingsamuel deleted the requirement-return-field-error branch January 25, 2021 13:40
timebertt added a commit to timebertt/gardener that referenced this pull request Jun 11, 2021
timebertt added a commit to timebertt/gardener that referenced this pull request Jun 14, 2021
timebertt added a commit to timebertt/gardener that referenced this pull request Jun 15, 2021
timebertt added a commit to timebertt/gardener that referenced this pull request Jun 16, 2021
timebertt added a commit to gardener/gardener that referenced this pull request Jun 16, 2021
* Upgrade to k/*@v0.21.1 in go.mod

* [automated] make revendor for k/* dependencies

This deletes pkg/mock/client-go/kubernetes/mocks.go to resolve the
following deadlock: make revendor fails because of some dependencies
of the file and make generate fails because of missing revendoring.
File will be generated again in the next commit.

* [automated] make generate for k/* dependencies

* Upgrade to c-r@v0.9.0 in go.mod

* [automated] make revendor for c-r dependency

`make revendor` results in `hack/setup-envtest.sh` being broken, so reset
the file after running `make revendor`. Adaption to breaking changes
in the upstream file will be done in a later commit.

* manager.NewClientBuilder was removed in favor of cluster.DefaultNewClient

ref kubernetes-sigs/controller-runtime#1409

* client.*MergeFrom* now take client.Object instead of runtime.Object

ref kubernetes-sigs/controller-runtime#1395

* [automated] make generate for c-r dependency

* Adapt to changes in labels.NewRequirement

ref kubernetes/kubernetes#97538

* Adapt to new setup-envtest tool

Makes use of the new setup-envtest tool (kubernetes-sigs/controller-runtime#1488)
in hack/setup-envtest.sh instead of vendoring hack/setup-envtest.sh and fetching
binaries with that.

* [automated] make revendor for setup-envtest tool

* Adapt pkg/envtest to upstream changes

- Make use of the new Users concept in envtest to provision a dedicated
  user for gardener-apiserver and a valid kubeconfig

- Make use of the new way to configure API server args to easily configure
  kube-aggregator flags

- Also generate certs for aggregation layer on our own instead of reusing
  the API server ca/certs (which is semantically correct), which allows us
  to drop our fork including kubernetes-sigs/controller-runtime#1449

* Styling nits
krgostev pushed a commit to krgostev/gardener that referenced this pull request Apr 21, 2022
* Upgrade to k/*@v0.21.1 in go.mod

* [automated] make revendor for k/* dependencies

This deletes pkg/mock/client-go/kubernetes/mocks.go to resolve the
following deadlock: make revendor fails because of some dependencies
of the file and make generate fails because of missing revendoring.
File will be generated again in the next commit.

* [automated] make generate for k/* dependencies

* Upgrade to c-r@v0.9.0 in go.mod

* [automated] make revendor for c-r dependency

`make revendor` results in `hack/setup-envtest.sh` being broken, so reset
the file after running `make revendor`. Adaption to breaking changes
in the upstream file will be done in a later commit.

* manager.NewClientBuilder was removed in favor of cluster.DefaultNewClient

ref kubernetes-sigs/controller-runtime#1409

* client.*MergeFrom* now take client.Object instead of runtime.Object

ref kubernetes-sigs/controller-runtime#1395

* [automated] make generate for c-r dependency

* Adapt to changes in labels.NewRequirement

ref kubernetes/kubernetes#97538

* Adapt to new setup-envtest tool

Makes use of the new setup-envtest tool (kubernetes-sigs/controller-runtime#1488)
in hack/setup-envtest.sh instead of vendoring hack/setup-envtest.sh and fetching
binaries with that.

* [automated] make revendor for setup-envtest tool

* Adapt pkg/envtest to upstream changes

- Make use of the new Users concept in envtest to provision a dedicated
  user for gardener-apiserver and a valid kubeconfig

- Make use of the new way to configure API server args to easily configure
  kube-aggregator flags

- Also generate certs for aggregation layer on our own instead of reusing
  the API server ca/certs (which is semantically correct), which allows us
  to drop our fork including kubernetes-sigs/controller-runtime#1449

* Styling nits
krgostev pushed a commit to krgostev/gardener that referenced this pull request Jul 5, 2022
* Upgrade to k/*@v0.21.1 in go.mod

* [automated] make revendor for k/* dependencies

This deletes pkg/mock/client-go/kubernetes/mocks.go to resolve the
following deadlock: make revendor fails because of some dependencies
of the file and make generate fails because of missing revendoring.
File will be generated again in the next commit.

* [automated] make generate for k/* dependencies

* Upgrade to c-r@v0.9.0 in go.mod

* [automated] make revendor for c-r dependency

`make revendor` results in `hack/setup-envtest.sh` being broken, so reset
the file after running `make revendor`. Adaption to breaking changes
in the upstream file will be done in a later commit.

* manager.NewClientBuilder was removed in favor of cluster.DefaultNewClient

ref kubernetes-sigs/controller-runtime#1409

* client.*MergeFrom* now take client.Object instead of runtime.Object

ref kubernetes-sigs/controller-runtime#1395

* [automated] make generate for c-r dependency

* Adapt to changes in labels.NewRequirement

ref kubernetes/kubernetes#97538

* Adapt to new setup-envtest tool

Makes use of the new setup-envtest tool (kubernetes-sigs/controller-runtime#1488)
in hack/setup-envtest.sh instead of vendoring hack/setup-envtest.sh and fetching
binaries with that.

* [automated] make revendor for setup-envtest tool

* Adapt pkg/envtest to upstream changes

- Make use of the new Users concept in envtest to provision a dedicated
  user for gardener-apiserver and a valid kubeconfig

- Make use of the new way to configure API server args to easily configure
  kube-aggregator flags

- Also generate certs for aggregation layer on our own instead of reusing
  the API server ca/certs (which is semantically correct), which allows us
  to drop our fork including kubernetes-sigs/controller-runtime#1449

* Styling nits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make nodeaffinity parsing functions return field.Error
7 participants